package com.example.profile_encryption.config;


import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class EncryptorCfg {

    //加密算法
    private static final String ALGRI = "PBEWITHHMACSHA512ANDAES_256";
    //哈希迭代次数
    private static final String ITERATIONS = "10000";

    private static final String worKey="work_key_value";
    private static final String rootKey="root_key_value";

    private String buildKey(){
        return SHAConfig.getAfterSha(rootKey,worKey);
    }

    //默认name="jasyptStringEncryptor" ，自定义时需要在配置文件配置jasypt.encryptor.bean=自定义名字
    @Bean(name="jasyptStringEncryptor")
    public StringEncryptor setConfig() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(buildKey());//自定义加密密钥， 密文加盐
        config.setAlgorithm(ALGRI);//设置加密算法
        config.setKeyObtentionIterations(ITERATIONS);//设置应用于获取加密密钥的哈希迭代次数
        config.setPoolSize("1");//设置要创建的加密程序池的大小
        config.setProviderName("SunJCE");//设置要请求加密算法的安全提供程序的名称
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");//设置 Sal 发生器
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");//设置 IV 发生器
        config.setStringOutputType("base64");//设置字符串输出的编码形式
        encryptor.setConfig(config);
        return encryptor;
    }
}
